// 参考自: https://segmentfault.com/q/1010000010946758
// 在mac的safari上使用localstorage时报了"the operation is insecure"的错误

import { message } from 'antd';

export function getAuthority() {
	let auth = 'guest';

  try {
		if (document.cookie && window.sessionStorage)
			auth = sessionStorage.getItem('antd-pro-authority');
  } catch(err) {
    message.error(err.toString());
  }

	return auth;
}

// 保存用户信息(用户登录)
export function setAuthority(params) {
	const { token, userId, name, userName, avatar, notifyCount } = params;
  sessionStorage.setItem('token', token);
  sessionStorage.setItem('userId', userId);
  sessionStorage.setItem('name', name);
  sessionStorage.setItem('userName', userName);
  if (avatar) sessionStorage.setItem('headPortrait', avatar);
  if (notifyCount) sessionStorage.setItem('notifyCount', notifyCount);
  sessionStorage.setItem('antd-pro-authority', 'admin');
}

// 清除用户信息(退出登录)
export function delAuthority() {
	sessionStorage.removeItem('token');
	sessionStorage.removeItem('userId');
	sessionStorage.removeItem('name');
	sessionStorage.removeItem('userName');
	sessionStorage.removeItem('headPortrait');
	sessionStorage.removeItem('notifyCount');
	sessionStorage.setItem('antd-pro-authority', 'guest');
}
